package edu.tgc.service.system;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.abyss.common.annotation.Log;
import org.abyss.common.service.CommonCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import edu.tgc.custom.EasyuiTree;
import edu.tgc.entity.system.SysDept;
import edu.tgc.entity.system.SysPosition;
import edu.tgc.entity.system.SysRole;
import edu.tgc.repository.system.SysDeptRepository;

@Log(value = "部门")
@Service
public class SysDeptService extends CommonCrudService<SysDept, UUID> {
    @Autowired
    private SysDeptRepository sysDeptRepository;

    public List<SysDept> findListByParent(SysDept dept) {
        return this.sysDeptRepository.findByParent(dept);
    }

    public List<SysDept> getListById(UUID id) {
        List<SysDept> list;
        if (id == null) {
            SysDept root = this.sysDeptRepository.findByParentIsNull();
            list = this.sysDeptRepository.findByParent(root);
        } else {
            list = this.sysDeptRepository.findByParentId(id);
        }
        return list;
    }

    public Set<SysPosition> getPositions(UUID id) {
        SysDept entity = this.baseRepository.findOne(id);
        if (entity != null) {
            return entity.getPositions();
        }
        return new HashSet<>();
    }

    @CacheEvict(allEntries = true, value = "default")
    public void setRoles(UUID id, Set<String> roleIds) {
        SysDept entity = this.baseRepository.findOne(id);
        HashSet<SysRole> roles = new HashSet<>();
        if (roleIds != null) {
            for (String roleId : roleIds) {
                roles.add(new SysRole(UUID.fromString(roleId)));
            }
        }
        entity.setRoles(roles);
        this.baseRepository.saveAndFlush(entity);
    }

    public List<EasyuiTree> subcompany() {
        SysDept root = this.sysDeptRepository.findByParentIsNull();
        List<SysDept> list = this.sysDeptRepository.findByParentId(root.getId());
        List<EasyuiTree> tree = new ArrayList<>();
        for (SysDept dept : list) {
            EasyuiTree node = new EasyuiTree();
            node.setId(dept.getId());
            node.setText(dept.getName());
            tree.add(node);
        }
        return tree;
    }

	public String getLeverl2DeptName() {
		//可填值为：\r\n
		String str="";
		SysDept root=sysDeptRepository.findByParentIsNull();
		List<SysDept> depts = sysDeptRepository.findByParent(root);
		for (SysDept dept : depts) {
			str+=dept.getName()+"\r\n";
		}
		if(str.length()>0){
			str=str.substring(0, str.length()-2);
		}
		str="可填值为：\r\n"+str;
		return str;
	}

    /*public List<EasyuiTree> getStartProjectDept() {
        SysDept root = this.sysDeptRepository.findByParentIsNull();
        List<EasyuiTree> tree = new ArrayList<>();
        for (SysDept dept : this.sysDeptRepository.findStartDept(root.getId())) {
            EasyuiTree node = new EasyuiTree();
            node.setId(dept.getId());
            node.setText(dept.getName());
            tree.add(node);
        }
        return tree;
    }*/

}
